package com.atguigu.gmall.payment.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.payment.service.WxPayService;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * projectName: gmall
 *
 * @author: 陈鹏
 * time: 2022/10/15 11:01 星期六
 * description: 微信支付的controller层
 */
@RestController
@RequestMapping(value = "/wx/pay")
public class WxPayController {

    @Resource
    private WxPayService wxPayService;

    /**
     * 获取支付二维码的地址
     * @param payMap
     * @return
     */
    @RequestMapping(value = "/getPayCodeUrl")
    public Map<String,String> getPayCodeUrl(@RequestParam Map<String, String> payMap){
        return wxPayService.getPayCodeUrl(payMap);
    }

    /**
     * 查询支付结果 :兜底方案
     * @param orderId
     * @return
     */
    @RequestMapping(value = "getPayResult")
    public Map<String,String> getPayResult(@RequestParam String orderId){
        return wxPayService.getPayResult(orderId);
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 微信通知地址的api,在用户支付完成后微信会调用这个接口
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/notify/address")
    public String notifyAddress(HttpServletRequest request) throws Exception{
/*        //获取通知的内容
        ServletInputStream is = request.getInputStream();
        //输出流初始化
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        //定义缓冲区
        byte[] buffer = new byte[1024];
        //定义len
        int len = 0;
        //遍历读
        while ((len = is.read(buffer)) != -1){
            os.write(buffer,0,len);
        }
        //解析内容
        byte[] bytes = os.toByteArray();
        //转换成字符串
        String xmlString = new String(bytes);
        //将字符串转换成map
        Map<String, String> result = WXPayUtil.xmlToMap(xmlString);
        //获取附加参数
        String attachString = result.get("attach");
        //反序列化
        Map<String, String> attach = JSONObject.parseObject(attachString, Map.class);
        //将支付结果通知订单微服务
        rabbitTemplate.convertAndSend(attach.get("exchange"), attach.get("routingKey"), JSONObject.toJSONString(result));*/

        String resultString = "{\"transaction_id\":\"4200001568202208169158755012\",\"nonce_str\":\"dd3293984914463aa6b436305301e8a8\",\"bank_type\":\"CMB_CREDIT\",\"openid\":\"oHwsHuIE5pkWXo64GtUN7drzqvHs\",\"sign\":\"A76FA38C0F17A7F53244B340EEB56D8D\",\"payway\":\"WX\",\"fee_type\":\"CNY\",\"mch_id\":\"1558950191\",\"cash_fee\":\"1\",\"out_trade_no\":\"9214493dd03844efbab72036abd17429\",\"appid\":\"wx74862e0dfcf69954\",\"total_fee\":\"1\",\"trade_type\":\"NATIVE\",\"result_code\":\"SUCCESS\",\"attach\":\"{\\\"exchange\\\":\\\"pay_exchange\\\",\\\"routingKey\\\":\\\"seckill.order.pay\\\",\\\"username\\\":\\\"cp\\\"}\",\"time_end\":\"20220816153320\",\"is_subscribe\":\"N\",\"return_code\":\"SUCCESS\"}";

        //将支付结果通知订单微服务
        Map<String, String> result = JSONObject.parseObject(resultString, Map.class);
        //获取附加参数
        String attachString = result.get("attach");
        //反序列化
        Map<String, String> attach = JSONObject.parseObject(attachString, Map.class);
        rabbitTemplate.convertAndSend(attach.get("exchange"), attach.get("routingKey"), resultString);

        //返回结果初始化
        HashMap<String, String> map = new HashMap<>();
        map.put("return_code", "SUCCESS");
        map.put("return_msg", "OK");
        //返回微信
        return WXPayUtil.mapToXml(map);
    }
}
